package common;

import java.util.HashMap;

import javax.swing.JPanel;
import javax.swing.JProgressBar;

import org.w3c.dom.Element;

import fitness.AmplyFitness;
import fitness.Fitness;
import fitness.MathFitness;

import optimizers.commons.RunConfig;
import optimizers.ganeat.Constants;
import optimizers.ganeat.GA;
import optimizers.ganeat.amplify.ConstantsAmply;
import optimizers.ganeat.amplify.GAAmplify;
import optimizers.ganeat.math.ConstantsMath;
import optimizers.ganeat.math.GAMath;
import optimizers.ganeat.sin.ConstantsSin;

public class RunMath extends RunStarter {
	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return "Math";
	}

	@Override
	public HashMap<String, String> getDefaultParameters() {
		HashMap<String, String> parameters = new HashMap<String, String>();
		parameters.put("Function", ConstantsMath.expression);
		parameters.put("Number of Test Points",
				String.valueOf(ConstantsMath.nTests));
		parameters.put("Min Test Input", String.valueOf(ConstantsMath.minTest));
		parameters.put("Max Test Input", String.valueOf(ConstantsMath.maxTest));
		parameters.put("Number of Display Points",
				String.valueOf(ConstantsMath.nDisplays));
		parameters.put("Min Display Input",
				String.valueOf(ConstantsMath.minDisplay));
		parameters.put("Max Display Input",
				String.valueOf(ConstantsMath.maxDisplay));
		parameters.put("Y-Scaling", String.valueOf(ConstantsMath.y_scaling));
		parameters.put("Max Scale", String.valueOf(ConstantsMath.max_scale));
		parameters.put("Reporter Concentration",
				String.valueOf(ConstantsMath.reporterConcentration));
		parameters.put("Stopping Relative Error",
				String.valueOf(ConstantsMath.stoppingError));
		parameters
				.put("Number of Inputs", String.valueOf(ConstantsMath.nInput));
		/*
		 * parameters.put("Input Concentrations",
		 * String.valueOf(ConstantsMath.inputConcentrations));
		 * parameters.put("Additional Input Concentration",
		 * String.valueOf(ConstantsMath.additionalinputConcentrations));
		 */
		return parameters;
	}

	@Override
	public void setParameters(HashMap<String, String> parameters) {
		try {
			ConstantsMath.expression = parameters.get("Function");
			ConstantsMath.nTests = Integer.parseInt(parameters
					.get("Number of Test Points"));
			ConstantsMath.minTest = Double.parseDouble(parameters
					.get("Min Test Input"));
			ConstantsMath.maxTest = Double.parseDouble(parameters
					.get("Max Test Input"));
			ConstantsMath.nDisplays = Integer.parseInt(parameters
					.get("Number of Display Points"));
			ConstantsMath.minDisplay = Double.parseDouble(parameters
					.get("Min Display Input"));
			ConstantsMath.maxDisplay = Double.parseDouble(parameters
					.get("Max Display Input"));
			ConstantsMath.y_scaling = Boolean.parseBoolean(parameters
					.get("Y-Scaling"));
			ConstantsMath.max_scale = Double.parseDouble(parameters
					.get("Max Scale"));
			ConstantsMath.reporterConcentration = Double.parseDouble(parameters
					.get("Reporter Concentration"));
			ConstantsMath.stoppingError = Double.parseDouble(parameters
					.get("Stopping Relative Error"));
			ConstantsMath.nInput = Integer.parseInt(parameters
					.get("Number of Inputs"));
			/*
			 * String[] inputs =
			 * parameters.get("Input Concentrations").split(","); double[]
			 * inputConcentrations = new double[inputs.length]; for (int i = 0;
			 * i < inputs.length; i++) { inputConcentrations[i] =
			 * Double.valueOf(inputs[i]); } ConstantsMath.inputConcentrations =
			 * inputConcentrations; inputs =
			 * parameters.get("Additional Input Concentration") .split(",");
			 * inputConcentrations = new double[inputs.length]; for (int i = 0;
			 * i < inputs.length; i++) { inputConcentrations[i] =
			 * Double.valueOf(inputs[i]); }
			 * ConstantsMath.additionalinputConcentrations =
			 * inputConcentrations;
			 */
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	@Override
	public void start() {
		runStatus.status = runStatus.RUNNING;
		for (int i = 0; i < Constants.nRuns; i++) {
			GAMath ga = (GAMath) getGA();
			try {
				ga.start();
			} catch (CloneNotSupportedException | InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	@Override
	public Fitness getFitnessFunction() {
		return new MathFitness();
	}

	@Override
	public GA getGA() {
		// TODO Auto-generated method stub
		return new GAMath(config, initIndiv, panelFitness, panelSpecies,
				panelIndiv, runStatus, progressBar, fitnessElement);
	}

}
